/*
   Copyright (c) 2011 Mizar Tools Contributors (mizartools.org)

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/
/*  Contributors :
 *	2011-02-05 Marco Riccardi - initial implementation
 *  2011-03-25 Marco Riccardi - implementation of visitor pattern
 *
 */
package org.mizartools.system.xml;

import java.util.LinkedList;

import org.mizartools.system.ElementParseException;
import org.mizartools.system.INode;
import org.mizartools.utility.xml.XMLElement;

public abstract class ReasoningItem implements INode {

    public static boolean isReasoningItem(String className){
        return SkeletonItem.isSkeletonItem(className) || AuxiliaryItem.isAuxiliaryItem(className);
    }
	
    public static ReasoningItem newInstance(INode parentNode, LinkedList<XMLElement> xmlElementList) throws ElementParseException {
        XMLElement xmlElement = xmlElementList.peek();
        if (!isReasoningItem(xmlElement.getName())){
            throw new ElementParseException(ElementParseException.NOT_VALID_ELEMENT, "XMLElement is a " + xmlElement.getName() + " not a subclass of ReasoningItem", xmlElementList);
        }
        if (SkeletonItem.isSkeletonItem(xmlElement.getName())){
        	return SkeletonItem.newInstance(parentNode, xmlElementList);
        } else 
        if (AuxiliaryItem.isAuxiliaryItem(xmlElement.getName())){
        	return AuxiliaryItem.newInstance(parentNode, xmlElementList);
        } else { 
        	throw new ElementParseException(ElementParseException.NOT_VALID_ELEMENT, "XMLElement is a " + xmlElement.getName() + " not a subclass of ReasoningItem", xmlElementList);
        }
    }

    public LinkedList<XMLElement> getXMLElementList() {
        if (this instanceof SkeletonItem) {
            return ((SkeletonItem) this).getXMLElementList();
        } else 
        if (this instanceof AuxiliaryItem) {
            return ((AuxiliaryItem) this).getXMLElementList();
        } else 
        return new LinkedList<XMLElement>();
    }
	
}
